package com.nineclock.document.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.mapper.DocCollaborationsMapper;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.pojo.DocCollaborations;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.service.PcDocumentService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PcDocumentServiceImpl implements PcDocumentService {
    @Autowired
    private DocFileMapper docFileMapper;

    @Autowired
    private SysUserFeign userFeign;

    @Autowired
    private DocCollaborationsMapper docCollaborationsMapper;

    @Override
    public PageResult<DocFileDto> pagingFile(Integer page, Integer pageSize, String keyword) {
        //分页查询
        LambdaQueryWrapper<DocFile> docFileQW = new LambdaQueryWrapper<>();
        docFileQW.eq(DocFile::getCompanyId, CurrentUserHolder.get().getCompanyId());
        if(StrUtil.isNotEmpty(keyword)){
            docFileQW.and(queryWrapper -> {
                queryWrapper.like(DocFile::getName, keyword)
                        .or().like(DocFile::getContent, keyword);
            });
        }
        Page<DocFile> docFilePage = new Page(page, pageSize);

        Page<DocFile> pages = docFileMapper.selectPage(docFilePage, docFileQW);
        List<DocFile> records = pages.getRecords();

        List<DocFileDto> docFileDtoList = BeanHelper.copyWithCollection(records, DocFileDto.class);

        //循环添加 创建者 协作者
        for (DocFileDto docFileDto : docFileDtoList) {
            SysCompanyUserDTO companyUserDTOCreate = userFeign.findUserById(docFileDto.getCreateUserId()).getData();

            docFileDto.setCreator(companyUserDTOCreate);

            LambdaQueryWrapper<DocCollaborations> docCollaborationsQW = new LambdaQueryWrapper<>();

            docCollaborationsQW.eq(DocCollaborations::getFileId, docFileDto.getId());

            List<DocCollaborations> docCollaborationsList = docCollaborationsMapper.selectList(docCollaborationsQW);

            List<Long> collaborationIds = docCollaborationsList.stream().map(docCollaboration -> {
                return docCollaboration.getCollaborationId();
            }).collect(Collectors.toList());

            List<SysCompanyUserDTO> collaborations = new ArrayList<>();

            if (CollectionUtil.isNotEmpty(collaborationIds)) {

                for (Long docCollaborationId : collaborationIds) {
                    SysCompanyUserDTO companyUserDTO = userFeign.findUserById(docCollaborationId).getData();
                    collaborations.add(companyUserDTO);
                }
            }

            docFileDto.setCollaborations(collaborations);
        }
        return new PageResult(pages.getTotal(),
                pages.getPages(), docFileDtoList);
    }

}
